package Controller.WebSocketController;

import Pojo.User;
import Service.CreditCardService;
import Utils.BaseMessage;
import com.alibaba.fastjson.JSON;

import java.sql.SQLException;
import javax.servlet.http.HttpSession;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(
        value = "/ws/CreditCardTispController",
        configurator = GetHttpSessionConfigurator.class
)
public class CreditCardTispController {
    private final CreditCardService creditCardService = new CreditCardService();
    HttpSession httpSession = null;

    public CreditCardTispController() {
    }

    @OnOpen
    public void onOpen(Session session, EndpointConfig config) {
        this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
        System.out.println(this.httpSession.getAttribute("name"));
    }

    @OnMessage
    public void onMessage(Session session, String message) {
        if (this.httpSession.getAttribute("userMsg") != null) {
            User user = (User) this.httpSession.getAttribute("userMsg");
            System.out.println(message);
            System.out.println("当前websocket加载成功");
            System.out.println(user);
            if (session != null) {
                try {
                    session.getAsyncRemote().sendText(JSON.toJSONString(new BaseMessage(0, (Object) null, this.creditCardService.getManagerCardCountBySeletive(user.getUserId(), (String) null, (String) null, (String) null, "1", (String) null))));
                } catch (SQLException var5) {
                    var5.printStackTrace();
                    session.getAsyncRemote().sendText(JSON.toJSONString(new BaseMessage(3, "出现异常,请联系管理员处理", (Object) null)));
                    return;
                }
            }

        }
    }
}
